#include "Graphic.h"

Graphic::Graphic(INT width, INT height, HWND hWnd) {
    _D3D = Direct3DCreate9(D3D_SDK_VERSION);
    if (_D3D == NULL)
        throw std::runtime_error("");

    D3DDISPLAYMODE displayMode;
    if (FAILED(_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
        throw std::runtime_error("");

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp{ 0 };

    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = displayMode.Format;

    // Create the D3DDevice
    if (FAILED(_D3D->CreateDevice(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        hWnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp,
        &_Device)))
    {
        throw std::runtime_error("");
    }

}

VOID Graphic::BeginFrame(DWORD r, DWORD g, DWORD b) {
    _Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(r, g, b), 1.0f, 0);
    _Device->BeginScene();
}

VOID Graphic::EndFrame() {
    _Device->EndScene();
    _Device->Present(NULL, NULL, NULL, NULL);
}

IDirect3DDevice9* Graphic::Device() {
    return _Device.Get();
}
